package utils

//访问百度api拿到商圈的信息

import java.io.UnsupportedEncodingException
import java.net.URLEncoder
import java.security.NoSuchAlgorithmException

import com.alibaba.fastjson.JSON
import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.commons.lang.StringUtils

import scala.collection.mutable

object GetBusiness {
  def getBusiness(lat:String,long:String)={
    val url = getUrl(lat,long)
    val client = new HttpClient()
    val method = new GetMethod(url)
    val status: Int = client.executeMethod(method)
    if (status == 200){
      val asString = method.getResponseBodyAsString
      parseJson(asString)
    }else ""
  }

  private def parseJson(str:String):String={
    val business = new StringBuffer()
    if (StringUtils.isNotEmpty(str)){
      val status = JSON.parseObject(str).getInteger("status")
      if (status == 0){
        val jSONObject = JSON.parseObject(str).getJSONObject("result")
        val bus = jSONObject.getString("business")
        business.append(bus)
        if (StringUtils.isEmpty(bus)){
          val array = jSONObject.getJSONArray("pois")
          for (i <- 0 until array.size()){
            val tag = array.getJSONObject(i).getString("tag")
            if (StringUtils.isNotEmpty(tag)) business.append(tag)
          }
        }
      }
    }
    business.toString
  }

  @throws[UnsupportedEncodingException]
  @throws[NoSuchAlgorithmException]
  private  def getUrl(lat:String,long:String): String = {
    // 计算sn跟参数对出现顺序有关，get请求请使用LinkedHashMap保存<key,value>，该方法根据key的插入顺序排序；post请使用TreeMap保存<key,value>，该方法会自动将key按照字母a-z顺序排序。所以get请求可自定义参数顺序（sn参数必须在最后）发送请求，但是post请求必须按照字母a-z顺序填充body（sn参数必须在最后）。以get请求为例：http://api.map.baidu.com/geocoder/v2/?address=百度大厦&output=json&ak=yourak，paramsMap中先放入address，再放output，然后放ak，放入顺序必须跟get请求中对应参数的出现顺序保持一致。
    val paramsMap = new mutable.LinkedHashMap[String, String]
    paramsMap.put("location", lat+","+long)
    paramsMap.put("output", "json")
    paramsMap.put("pois", "1")
    paramsMap.put("ak", "XGB3Wff5IfxGURWQe09zaKOkcpm0EtcN")
    // 调用下面的toQueryString方法，对LinkedHashMap内所有value作utf8编码，拼接返回结果address=%E7%99%BE%E5%BA%A6%E5%A4%A7%E5%8E%A6&output=json&ak=yourak
    val paramsStr = toQueryString(paramsMap)
    // 对paramsStr前面拼接上/geocoder/v2/?，后面直接拼接yoursk得到/geocoder/v2/?address=%E7%99%BE%E5%BA%A6%E5%A4%A7%E5%8E%A6&output=json&ak=yourakyoursk
    val wholeStr = new String("/geocoder/v2/?" + paramsStr + "UTwnZmeyG2muhzEGbKe1RWIGLz7dRLCR")
    // 对上面wholeStr再作utf8编码
    val tempStr = URLEncoder.encode(wholeStr, "UTF-8")
    // 调用下面的MD5方法得到最后的sn签名7de5a22212ffaa9e326444c75a58f9a0
    val sn = MD5(tempStr)
    "http://api.map.baidu.com/geocoder/v2/?"+paramsStr+"&sn="+sn
  }

  // 对Map内所有value作utf8编码，拼接返回结果
  @throws[UnsupportedEncodingException]
  private def toQueryString(data: mutable.LinkedHashMap[String, String]): String = {
    val queryString = new StringBuffer
    import scala.collection.JavaConversions._
    for (pair <- data.entrySet) {
      queryString.append(pair.getKey + "=")
      queryString.append(URLEncoder.encode(pair.getValue, "UTF-8") + "&")
    }
    if (queryString.length > 0) queryString.deleteCharAt(queryString.length - 1)
    queryString.toString
  }

  // 来自stackoverflow的MD5计算方法，调用了MessageDigest库函数，并把byte数组结果转换成16进制
  private def MD5(md5: String): String = {
    try {
      val md = java.security.MessageDigest.getInstance("MD5")
      val array = md.digest(md5.getBytes)
      val sb = new StringBuffer
      var i = 0
      while ( {
        i < array.length
      }) {
        sb.append(Integer.toHexString((array(i) & 0xFF) | 0x100).substring(1, 3))

        {
          i += 1;
          i
        }
      }
      return sb.toString
    } catch {
      case e: NoSuchAlgorithmException =>

    }
    null
  }
}
